#include "format.hpp"
#include "sink.hpp"
#include "logger.hpp"
#include "buffer.hpp"

int main()
{
  //读取文件数据，一点一点写入缓冲区，最终将缓冲区数据写入文件，判断生成的新文件与源文件是否一致
  // 通过 md5值确定文件中的数据是否一致，md5sum
  std::ifstream ifs("./logfile/test.log", std::ios::binary);
  if(ifs.is_open() == false) 
  {
    std::cout << "open faild!\n";
    return -1;
  }
  ifs.seekg(0,std::ios::end);//读写位置跳转到文件结尾
  size_t fsize = ifs.tellg();//获取当前读写位置相对于起始位置的偏移量
  ifs.seekg(0,std::ios::beg);//重新跳转到起始位置
  std::string body;
  body.resize(fsize);
  ifs.read(&body[0], fsize);
  if (ifs.good() == false)
  {
    std::cout << "read error\n"; return -1;
  }
  ifs.close();

  qywlog::Buffer buffer;
  for(int i = 0; i < body.size(); i++)
  {
    buffer.push(&body[i],1);
  }
  size_t rsize = buffer.readAbleSize();
  std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
  for(int i = 0; i < rsize; i++)
  {
    ofs.write(buffer.begin(), 1);
    buffer.moveReader(1);
  }

  return 0;
}
